from PySide6.QtCore import QUrl
from PySide6.QtGui import QDesktopServices
from PySide6.QtWidgets import QWidget, QPushButton, QHBoxLayout

from common.player.reference import Player
from game.save_interface import game_save
from gui.reference import R_SIDE_LEFT, QUICK_BAR_TOP, R_SIDE_WIDTH, QUICK_BAR_HEIGHT
from gui.widgets_map import open_window


class QuickBarWidget(QWidget):
    def __init__(self, parent, player: Player):
        super().__init__(parent)
        self.setGeometry(
            R_SIDE_LEFT,
            QUICK_BAR_TOP,
            R_SIDE_WIDTH,
            QUICK_BAR_HEIGHT
        )

        self.player = player

        layout = QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)

        self.btn_bag = QPushButton(player.bag.title, self)
        self.btn_bag.clicked.connect(lambda: open_window('bag', player))

        self.btn_comm = QPushButton('信息态', self)
        self.btn_comm.setEnabled(False)

        self.btn_quest = QPushButton(self.player.quest_book.title, self)
        self.btn_quest.clicked.connect(lambda: open_window('dialog_quest', player))

        self.btn_save = QPushButton('存档', self)
        self.btn_save.clicked.connect(lambda: game_save(self.player))

        self.btn_guide = QPushButton('帮助文档', self)
        self.btn_guide.clicked.connect(
            lambda: QDesktopServices.openUrl(QUrl('https://www.showdoc.com.cn/BuildSuperWarrior/11558944258043778'))
        )

        layout.addWidget(self.btn_bag)
        layout.addWidget(self.btn_quest)
        layout.addWidget(self.btn_comm)
        layout.addWidget(self.btn_save)
        layout.addWidget(self.btn_guide)

    def update_data(self):
        self.btn_quest.setText(self.player.quest_book.title)
        self.btn_bag.setText(self.player.bag.title)
        self.update()
